﻿using System;
using System.Collections.Generic;
using System.ComponentModel.DataAnnotations;
using System.Linq;
using System.Web;

namespace SilentVoice.Models
{
    public partial class ScratchPad : Auditable
    {
        public ScratchPad()
        {
            Notes = "";
            CreatedAt = DateTime.Now;
        }

        [Key]
        public int Id { get; set; }
        [Required]
        public string Notes { get; set; }

       // [Required]
      //  public virtual Event Event { get; set; }
        public int EventId { get; set; }
      //  [Required]
      //  public virtual User Owner { get; set; }
        public int OwnerId { get; set; }

        //TODO: This might have to be enforced at the business layer level
        /*
        public IEnumerable<ValidationResult> IsValid(ValidationContext validationContext)
        {
            var notesContext = validationContext.Items["Context"];

            //ScratchPad unique for Event
            if (notesContext.ScratchPads.Any(x => x.Event.Equals(Event) && x.Owner.Equals(Owner)))
            {
                yield return new ValidationResult("A ScratchPad for this Event already exists for this User!", new[] {"Event", "Owner"});
            }


            yield break;
        }*/

        public override bool Equals(object obj)
        {
            if (obj == null || obj.GetType().DeclaringType != this.GetType().DeclaringType)
            {
                System.Console.WriteLine("they're not the same type, {0} {1}", obj.GetType().DeclaringType.ToString(), this.GetType().DeclaringType.ToString());
                return false;
            }

            ScratchPad other = (ScratchPad)obj;

            return this.Notes.Equals(other.Notes)
                && this.EventId == other.EventId
                && this.OwnerId == other.OwnerId;
        }

        public override int GetHashCode()
        {
            return ("" + this.Notes.GetHashCode() + "|" + this.OwnerId.GetHashCode()).GetHashCode();
        }
    }
}